﻿using MCUPackage;
using MCUPackage.Utils;
using System;
using System.ComponentModel;

namespace SxPackages.STC32G.Models
{
	[Serializable]
	class PWMGrpOptionBase
	{
		protected STC32GQFP64 mParent = null;

		/// <summary>
		/// 分组, 1:A 2:B
		/// </summary>
		[Browsable(false)]
		public char Group { get; protected set; } = 'A';

		private int mPSCR = 1;
		[DisplayName("分频系数"),
		DefaultValue(1),
		Description("本组PWM的分频系数, 与下面的周期时间共同决定PWM的输出频率, 取值范围1~65536")]
		public int PSCR
		{
			get => mPSCR;
			set
			{
				if (value != mPSCR)
				{
					if (value < 1 || value > 65536)
					{
						return;
					}
					mPSCR = value;
					CalFreq();
				}
			}
		}

		private int mPeriod = 2047;
		[DisplayName("周期时间"),
		DefaultValue(2047),
		Description("本组PWM的输出周期时间, 与上面的分频系数共同决定PWM的输出频率, 取值范围1~65536")]
		public int Period
		{
			get => mPeriod;
			set
			{
				if (value != mPeriod)
				{
					if (value < 1 || value > 65536)
					{
						return;
					}
					mPeriod = value;
					int index = this.Group == 'A' ? 0 : 4;
					for (int i = 0; i < 4; i++)
					{
						var opt = mParent.mPWMs.All[index];
						if (opt.Enable)
						{
							if (opt.Duty > value)
							{
								opt.Duty = value;
							}
						}
						index++;
					}
					CalFreq();
				}
			}
		}

		private int mDead = 0;
		[DisplayName("死区时间"),
		DefaultValue(0),
		Description("本组PWM的死区时间")]
		public virtual int Dead
		{
			get => mDead;
			set
			{
				if (value != mDead)
				{
					if (value < 0 || value > 255)
					{
						return;
					}
					mDead = value;
				}
			}
		}

		[DisplayName("使能计数器"),
		DefaultValue(true),
		Description("是否使能本组PWM计数器")]
		public bool EnableCEN { get; set; } = true;

		[DisplayName("使能主输出"),
		DefaultValue(true),
		Description("是否使能本组PWM主输出")]
		public bool EnableOut { get; set; } = true;

		[DisplayName("中断优先级"),
		Description("中断优先级等级设置, PWM的中断默认是禁用的, 需要的话在配置函数中自己使能即可"),
		DefaultValue(PriorityTypes.最低级)]
		public PriorityTypes Priority { get; set; } = PriorityTypes.最低级;

		/// <summary>
		/// 计算输出频率
		/// </summary>
		/// <returns></returns>
		public void CalFreq()
		{
			UInt64 tmp = (UInt64)(PSCR) * (UInt64)(Period);
			double freq = mParent.mSYSTEM.FreqValue / tmp;

			mParent.UpdateMsg($"PWM输出频率: {freq:F2}KHz");
		}

		public PWMGrpOptionBase(STC32GQFP64 aParent)
		{
			this.mParent = aParent;
		}

		public override String ToString()
		{
			return string.Empty;
		}

		/// <summary>
		/// 组初始化模板
		/// </summary>
		private const string TEMPLATE =
@"	attr.PWM_Period        = {0};					//周期时间,   0~65535
	attr.PWM_DeadTime      = {1};						//死区发生器设置, 0~255
	attr.PWM_MainOutEnable = {2};				//主输出使能, ENABLE,DISABLE
	attr.PWM_CEN_Enable    = {3};				//使能计数器, ENABLE,DISABLE
	PWM_Configuration(PWM{4}, &attr);				//初始化PWM通用寄存器,  PWMA,PWMB
	NVIC_PWM_Init(PWM{4}, DISABLE, {5});";

		public string Generate()
		{
			return string.Format(TEMPLATE, this.Period - 1, this.Dead, this.EnableOut.ToEnable(),
								this.EnableCEN.ToEnable(), this.Group, this.Priority.ToDescription());
		}
	}
}
